package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.dto.Approval.EndRunTaskDto;
import com.example.entity.yifei.*;
import com.example.entity.ymlConfig.YmlConfig;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.service.CmsmqService;
import com.example.service.CopteService;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/approval")
@Slf4j
@Api(tags = "审批流程表单接口")
public class ApprovalController {
    @Resource
    private CmsmqService cmsmqService;
    @Autowired
    private CmsmqMapper cmsmqMapper;
    @Autowired
    private CoptcMapper coptcMapper;
    @Autowired
    private CoptdMapper coptdMapper;
    @Autowired
    private PurtcMapper purtcMapper;
    @Autowired
    private PurtdMapper purtdMapper;
    @Resource
    private PurtaMapper purtaMapper;
    @Resource
    private PurtbMapper purtbMapper;
    @Resource
    private PurtlMapper purtlMapper;
    @Resource
    private PurtmMapper purtmMapper;
    @Resource
    private MoctmMapper moctmMapper;
    @Resource
    private MoctnMapper moctnMapper;
    @Resource
    private CoptaMapper coptaMapper;
    @Resource
    private CoptbMapper coptbMapper;
    @Resource
    private InvtlMapper invtlMapper;
    @Resource
    private InvtmMapper invtmMapper;
    @Resource
    private CopteService copteService;
    @Resource
    private CoptfMapper coptfMapper;
    @Resource
    private CoptnMapper coptnMapper;
    @Resource
    private CoptoMapper coptoMapper;
    @Resource
    private CmsniMapper cmsniMapper;
    @Resource
    private CopteMapper copteMapper;

//    @PostMapping("/approvalCancel")
    public Result approvalCancel(@RequestBody String id) {
        if (StringUtils.isEmpty(id)) {
            return Result.error("参数不能为空");
        }
        String[] strings = id.split(":");
        if (strings.length != 2) {
            return Result.error("参数格式错误：单别:单号");
        }
        Cmsmq cmsmq = cmsmqMapper.selectOne(new LambdaQueryWrapper<Cmsmq>()
                .eq(Cmsmq::getMq001, strings[0]));
        if (cmsmq == null) {
            return Result.error("单别不存在");
        }
        if ("22".equals(cmsmq.getMq003())) {   // 销售订单
            Coptc coptc = coptcMapper.selectOne(new LambdaQueryWrapper<Coptc>()
                    .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
            if (coptc == null) {
                return Result.error("该销售订单不存在");
            }
            coptc.setTc048("0");
            coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>()
                    .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
        } else if ("33".equals(cmsmq.getMq003())) {  // 采购单
            Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>()
                    .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
            if (purtc == null) {
                return Result.error("该采购订单不存在");
            }
            purtc.setTc030("0");
            purtcMapper.update(purtc, new LambdaQueryWrapper<Purtc>()
                    .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
        } else {
            return Result.error("非采购/销售单别");
        }
        return Result.success("成功");
    }

    @Log("审批开始时触发")
    @ApiOperation("审批开始时触发")
    @PostMapping("/approvalStart")
    public Result approvalStart(@RequestBody String id) {
        log.info("审批开始触发业务操作：id={}",id);
        if (StringUtils.isEmpty(id)) {
            return Result.error("参数不能为空");
        }
        String[] strings = id.split(":");
//        if (strings.length != 2) {
//            return Result.error("参数格式错误：单别:单号");
//        }
        if(strings.length==2){
            Cmsmq cmsmq = cmsmqMapper.selectOne(new LambdaQueryWrapper<Cmsmq>()
                    .eq(Cmsmq::getMq001, strings[0]));
            if (cmsmq == null) {
                return Result.error("单别不存在");
            }
            if ("22".equals(cmsmq.getMq003())) {   // 销售订单
                Coptc coptc = coptcMapper.selectOne(new LambdaQueryWrapper<Coptc>()
                        .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
                if (coptc == null) {
                    return Result.error("该销售订单不存在");
                }
                // 修改签核状态码，状态到 签核中
                coptc.setTc048("1");
                coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>()
                        .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
            } else if ("33".equals(cmsmq.getMq003())) {  // 采购单
                Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>()
                        .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
                if (purtc == null) {
                    return Result.error("该采购订单不存在");
                }
                purtc.setTc030("1"); // 签核中
                purtcMapper.update(purtc, new LambdaQueryWrapper<Purtc>()
                        .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
            } else if ("31".equals(cmsmq.getMq003())){
                Purta purta = purtaMapper.selectOne(new LambdaQueryWrapper<Purta>()
                        .eq(Purta::getTa001, strings[0]).eq(Purta::getTa002, strings[1]));
                if (purta==null){
                    return Result.error("该请购订单不存在");
                }
                purta.setTa016("1");// 签核中
                purtaMapper.update(purta,new LambdaQueryWrapper<Purta>()
                        .eq(Purta::getTa001, strings[0]).eq(Purta::getTa002, strings[1]));
            }else if ("32".equals(cmsmq.getMq003())){
                Purtl purtl = purtlMapper.selectOne(new LambdaQueryWrapper<Purtl>()
                        .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
                if (purtl==null){
                    return Result.error("该核价单不存在");
                }
                purtl.setTL012("1");// 签核中
                purtlMapper.update(purtl,new LambdaQueryWrapper<Purtl>()
                        .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
            }else if ("5B".equals(cmsmq.getMq003())){
                Moctm moctm = moctmMapper.selectOne(new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
                if (moctm==null){
                    return Result.error("该委外核价单不存在");
                }
                moctm.setTM012("1");// 签核中
                moctmMapper.update(moctm, new LambdaQueryWrapper<Moctm>()
                        .eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
            }else if ("21".equals(cmsmq.getMq003())){
                Copta copta = coptaMapper.selectOne(new LambdaQueryWrapper<Copta>()
                        .eq(Copta::getTA001, strings[0]).eq(Copta::getTA002, strings[1]));
                if(copta == null) {
                    throw new CustomException("500", "报价单不存在");
                }
                copta.setTA029("1");
                coptaMapper.update(copta, new LambdaQueryWrapper<Copta>()
                        .eq(Copta::getTA001, strings[0]).eq(Copta::getTA002, strings[1]));
            }else if ("18".equals(cmsmq.getMq003())) {
                Invtl invtl = invtlMapper.selectOne(new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
                if(invtl == null) {
                    throw new CustomException("500", "报废单不存在");
                }
                invtl.setTL017("1");
                invtlMapper.update(invtl, new LambdaQueryWrapper<Invtl>()
                        .eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
            }else if ("17".equals(cmsmq.getMq003())){
                System.out.println("进入存货调整单审核 开始");
            }else if ("AMU1".equals(strings[0])){
                System.out.println("进入材料领料单审核 开始");
            }else if ("AMUB".equals(strings[0])){
                System.out.println("进入材料补料单审核 开始");
            }else if ("AMR1".equals(strings[0])){
                System.out.println("进入材料退料单审核 开始");
            }
            else {
                return Result.error("非采购/销售/请购/核价/委外核价/报价单/报废单单别");
            }
        }else if(strings.length==3){
            log.info("销售变更单开始:"+strings[0]+strings[1]+strings[2]);
            Copte copte = copteMapper.selectOne(new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, strings[0]).eq(Copte::getTE002, strings[1]).eq(Copte::getTE003, strings[2]));
            if(copte == null) {
                throw new CustomException("500", "销售变更单不存在");
            }
            copte.setTE044("1");
            copteMapper.update(copte, new LambdaQueryWrapper<Copte>()
                    .eq(Copte::getTE001, strings[0]).eq(Copte::getTE002, strings[1]).eq(Copte::getTE003, strings[2]));
            return Result.success("成功");
        }
        return Result.success("成功");
    }

    @Log("审批结束后触发")
    @ApiOperation("审批结束后触发")
    @PostMapping("/approvalEnd")
    @DSTransactional
    public Result approvalEnd(@RequestBody EndRunTaskDto dto) {
        log.info("审批开始触发业务操作：EndRunTaskDto={}",dto);
        if (StringUtils.isEmpty(dto.getId()) || dto.getState() == null) {
            return Result.error("参数不能为空");
        }
        String[] strings = dto.getId().split(":");
        if (strings.length != 2) {
            return Result.error("参数格式错误：单别:单号");
        }
        Cmsmq cmsmq = cmsmqMapper.selectOne(new LambdaQueryWrapper<Cmsmq>()
                .eq(Cmsmq::getMq001, strings[0]));
        if (cmsmq == null) {
            return Result.error("单别不存在");
        }
        if ("22".equals(cmsmq.getMq003())) {   // 销售订单
            Coptc coptc = coptcMapper.selectOne(new LambdaQueryWrapper<Coptc>()
                    .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
            if (coptc == null) {
                return Result.error("该销售订单不存在");
            }
            if (dto.getState()) {   // 审核成功处理
                coptc.setTc048("3");
                coptc.setTc040(dto.getAssignee());  // 审核者
                checkCoptc(coptc, coptc.getTc001(), coptc.getTc002());
//                saveCoptn(coptc);
            } else {   // 审核失败处理
                // 修改签核状态码，状态到 退件
                coptc.setTc048("2");
                coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>()
                        .eq(Coptc::getTc001, strings[0]).eq(Coptc::getTc002, strings[1]));
            }
        } else if ("33".equals(cmsmq.getMq003())) {  // 采购单
            Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>()
                    .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
            if (purtc == null) {
                return Result.error("该采购订单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                purtc.setTc030("3");
                purtc.setTc025(dto.getAssignee());  // 审核者
                checkPurtc(purtc, purtc.getTc001(), purtc.getTc002());
            } else {  // 审核失败处理
                purtc.setTc030("2");
                purtcMapper.update(purtc, new LambdaQueryWrapper<Purtc>()
                        .eq(Purtc::getTc001, strings[0]).eq(Purtc::getTc002, strings[1]));
            }
        } else if ("31".equals(cmsmq.getMq003())){
            Purta purta = purtaMapper.selectOne(new LambdaQueryWrapper<Purta>()
                    .eq(Purta::getTa001, strings[0]).eq(Purta::getTa002, strings[1]));
            if (purta==null){
                return Result.error("该请购订单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                purta.setTa007("3");
                purta.setTa014(dto.getAssignee());  // 审核者
                checkPurta(purta, purta.getTa001(), purta.getTa002());
            } else {  // 审核失败处理
                purta.setTa016("2");
                purtaMapper.update(purta,new LambdaQueryWrapper<Purta>()
                        .eq(Purta::getTa001, strings[0]).eq(Purta::getTa002, strings[1]));
            }
        }else if ("32".equals(cmsmq.getMq003())){
            Purtl purtl = purtlMapper.selectOne(new LambdaQueryWrapper<Purtl>()
                    .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
            if (purtl==null){
                return Result.error("该核价单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                purtl.setTL012("3");
                purtl.setTL011(dto.getAssignee());  // 审核者
                checkPurtl(purtl, purtl.getTL001(), purtl.getTL002());
            } else {  // 审核失败处理
                purtl.setTL012("2");// 退件
                purtlMapper.update(purtl,new LambdaQueryWrapper<Purtl>()
                        .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
            }
        }else if ("5B".equals(cmsmq.getMq003())){
            Moctm moctm = moctmMapper.selectOne(new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
            if (moctm==null){
                return Result.error("该委外核价单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                moctm.setTM012("3");
                moctm.setTM011(dto.getAssignee());  // 审核者
                checkMoctm(moctm, moctm.getTM001(), moctm.getTM002());
            } else {  // 审核失败处理
                moctm.setTM012("2");// 退件
                moctmMapper.update(moctm, new LambdaQueryWrapper<Moctm>()
                        .eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
            }
        }else if ("21".equals(cmsmq.getMq003())){
            System.out.println("进入报价单 结束");
            Copta copta = coptaMapper.selectOne(new LambdaQueryWrapper<Copta>().eq(Copta::getTA001, strings[0]).eq(Copta::getTA002, strings[1]));
            if(copta == null) {
                throw new CustomException("500", "报价单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                copta.setTA029("3");
                copta.setTA015(dto.getAssignee());  // 审核者
                checkCopTa(copta, copta.getTA001(), copta.getTA002());
            } else {  // 审核失败处理
                copta.setTA029("2");// 退件
                coptaMapper.update(copta, new LambdaQueryWrapper<Copta>()
                        .eq(Copta::getTA001, strings[0]).eq(Copta::getTA002, strings[1]));
            }
        }else if ("18".equals(cmsmq.getMq003())) {
            Invtl invtl = invtlMapper.selectOne(new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
            if(invtl == null) {
                throw new CustomException("500", "报废单不存在");
            }
            if (dto.getState()) {  // 审核成功处理
                invtl.setTL017("3");
                invtl.setTL013(dto.getAssignee());  // 审核者
                checkInvtl(invtl, invtl.getTL001(), invtl.getTL002());
            } else {  // 审核失败处理
                invtl.setTL017("2");// 退件
                invtlMapper.update(invtl, new LambdaQueryWrapper<Invtl>()
                        .eq(Invtl::getTL001, strings[0]).eq(Invtl::getTL002, strings[1]));
            }
        }else if (strings[0].equals("ASOC")||strings[0].equals("ASO")){
            System.out.println("进入销售变更单审核 结束");
            if(dto.getState()) { // 通过时
                if(StringUtils.isNotEmpty(dto.getId())) {
                    String[] split = dto.getId().split(":");
                    if(split.length == 3) {
                        copteService.copteInsertCoptc(split[0], split[1], split[2], dto.getAssignee());
                    }else{
                        throw new CustomException("500", "审核返回的, 销售变更单单号错误!");
                    }
                }else{
                    throw new CustomException("500", "审核返回的, 销售变更单单号为空!");
                }
            }else{  // 退回时
                if(StringUtils.isNotEmpty(dto.getId())) {
                    String[] split = dto.getId().split(":");
                    if(split.length == 3) {
                        Copte copte11 = new Copte();
                        copte11.setTE044("2");
                        copteMapper.update(copte11, new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, split[0]).eq(Copte::getTE002, split[1]).eq(Copte::getTE003, split[2]));
                    }else{
                        throw new CustomException("500", "审核返回的, 销售变更单单号错误!");
                    }
                }else{
                    throw new CustomException("500", "审核返回的, 销售变更单单号为空!");
                }

            }
        }else if ("17".equals(cmsmq.getMq003())){
            System.out.println("进入存货调整单审核 结束");
        }else if ("AMU1".equals(strings[0])){
            System.out.println("进入材料领料单审核 结束");
        }else if ("AMUB".equals(strings[0])){
            System.out.println("进入材料补料单审核 结束");
        }else if ("AMR1".equals(strings[0])){
            System.out.println("进入材料退料单审核 结束");
        }
        else {
            return Result.error("非采购/销售/请购/核价/委外核价/报价单/报废单单别");
        }
        return Result.success("成功");
    }

    private void checkInvtl(Invtl invtl, String TC001, String TC002) {
        invtl.setTL014("Y");
        invtlMapper.update(invtl, new LambdaQueryWrapper<Invtl>().eq(Invtl::getTL001, TC001).eq(Invtl::getTL002, TC002));
        Invtm invtm = new Invtm();
        invtm.setTM021("Y");
        invtmMapper.update(invtm, new LambdaQueryWrapper<Invtm>().eq(Invtm::getTM001, TC001).eq(Invtm::getTM002, TC002));
    }

    /**
     * 修改报价单单头和单身的审核状态
     * @param copta
     * @param TC001
     * @param TC002
     */
    private void checkCopTa(Copta copta, String TC001, String TC002) {
        copta.setTA019("Y");
        coptaMapper.update(copta, new LambdaQueryWrapper<Copta>().eq(Copta::getTA001, TC001).eq(Copta::getTA002, TC002));
        Coptb coptb = new Coptb();
        coptb.setTb011("Y");
        coptbMapper.update(coptb, new LambdaQueryWrapper<Coptb>().eq(Coptb::getTb001, TC001).eq(Coptb::getTb002, TC002));
    }

    /**
     * 修改委外核价单的单头和单身的审核状态
     * @param moctm
     * @param TC001
     * @param TC002
     */
    private void checkMoctm(Moctm moctm, String TC001, String TC002) {
        moctm.setTM007("Y");
        moctmMapper.update(moctm, new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, TC001).eq(Moctm::getTM002, TC002));
        Moctn moctn = new Moctn();
        moctn.setTN014("Y");
        moctnMapper.update(moctn, new LambdaQueryWrapper<Moctn>().eq(Moctn::getTN001, TC001).eq(Moctn::getTN002, TC002));
    }


    /**
     * 修改请购单单头单身审核状态和签核状态
     * @param purta
     * @param TC001
     * @param TC002
     */
    private void checkPurta(Purta purta, String TC001, String TC002) {
        purta.setTa007("Y");
        purtaMapper.update(purta, new LambdaQueryWrapper<Purta>().eq(Purta::getTa001, TC001).eq(Purta::getTa002, TC002));
        Purtb purtb = new Purtb();
        purtb.setTb025("Y");
        purtbMapper.update(purtb, new LambdaQueryWrapper<Purtb>().eq(Purtb::getTb001, TC001).eq(Purtb::getTb002, TC002));
    }

    /**
     * 修改核价单单头单身审核码
     * @param purtl
     * @param TC001
     * @param TC002
     */
    private void checkPurtl(Purtl purtl, String TC001, String TC002) {
        purtl.setTL006("Y");
        purtlMapper.update(purtl, new LambdaQueryWrapper<Purtl>().eq(Purtl::getTL001, TC001).eq(Purtl::getTL002, TC002));
        Purtm purtm = new Purtm();
        purtm.setTm001("Y");
        purtmMapper.update(purtm, new LambdaQueryWrapper<Purtm>().eq(Purtm::getTm001, TC001).eq(Purtm::getTm002, TC002));
    }

    /**
     * 修改采购单单头单身审核状态和签核状态
     * @param purtc
     * @param TC001
     * @param TC002
     */
    private void checkPurtc(Purtc purtc, String TC001, String TC002) {
        purtc.setTc014("Y");
        purtcMapper.update(purtc, new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        Purtd purtd = new Purtd();
        purtd.setTd018("Y");
        purtdMapper.update(purtd, new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd001, TC001).eq(Purtd::getTd002, TC002));
    }

    /**
     * 修改销售单单头单身审核状态和签核状态
     * @param coptc
     * @param TC001
     * @param TC002
     */
    public void checkCoptc(Coptc coptc, String TC001, String TC002) {
        coptc.setTc027("Y");
        coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>().eq(Coptc::getTc001, TC001).eq(Coptc::getTc002, TC002));
        Coptd coptd = new Coptd();
        coptd.setTd021("Y");
        coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TC001).eq(Coptd::getTd002, TC002));
    }

    /**
     * 销售订单审核通过后自动生成出货通知单
     * @param coptc 审核通过传参
     */
//    @Async
//    @Transactional
    public void saveCoptn(Coptc coptc){
        log.info("自动新增出货通知单触发");
        try {
            // 新增出货通知单单头
            Coptn coptn = new Coptn();
            coptn.setCOMPANY(YmlConfig.getYFCOMPANY());
            coptn.setCREATE_DATE(TimeUtils.getNowTime());
            coptn.setFLAG(1);
            coptn.setCREATOR(coptc.getCreator());
            coptn.setTN001("ASH");
            coptn.setTN002(cmsmqService.getIdByType("ASH", "coptn"));
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
            coptn.setTN003(dateFormat.format(new Date()));
            coptn.setTN004(coptc.getTc004());
            coptn.setTN005("");
            coptn.setTN006(coptc.getTc005());
            coptn.setTN007(coptc.getTc006());
            coptn.setTN008(coptc.getTc004());
            coptn.setTN009(coptc.getTc010());
            coptn.setTN010(coptc.getTc011());
            coptn.setTN011(coptc.getTc016());
            coptn.setTN012(coptc.getREMARK());
            coptn.setTN013(coptc.getTc008());
            coptn.setTN014(coptc.getTc009());
            coptn.setTN015(new BigDecimal("0.0"));
            coptn.setTN016("Y");
            coptn.setTN017(coptc.getTc007());
            coptn.setTN018("");
            coptn.setTN019(new BigDecimal("0.0"));
            coptn.setTN020(0);
            coptn.setTN021(coptc.getTc039());
            coptn.setTN022("");
            coptn.setTN023(coptc.getTc042());
            coptn.setTN024("N");
            coptn.setTN025("");
            coptn.setTN026("");
            coptn.setTN027("");
            coptn.setTN028("");
            coptn.setTN029(0);
            coptn.setTN030(coptc.getTc041());
            coptn.setTN031("");
            coptn.setTN032("");
            coptn.setTN033("");
            coptn.setTN034(coptc.getTc031());
            coptn.setTN035(new BigDecimal("0.0"));
            coptn.setTN036(new BigDecimal("0.0"));
            coptn.setTN037("N");
            coptn.setTN038(coptc.getTc077());

            this.coptnMapper.insert(coptn);

            // 新增出货通知单单身
            List<Coptd> coptdList = this.coptdMapper.selectList(new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, coptc.getTc001()).eq(Coptd::getTd002, coptc.getTc002()));
            if (!coptdList.isEmpty()){
                coptdList.forEach(arr -> {
                    Copto copto = new Copto();
                    copto.setCOMPANY(YmlConfig.getYFCOMPANY());
                    copto.setCREATE_DATE(TimeUtils.getNowTime());
                    copto.setFLAG(1);
                    copto.setCREATOR(coptc.getCreator());
                    copto.setTO001(coptn.getTN001());
                    copto.setTO002(coptn.getTN002());
                    copto.setTO003("000"+String.valueOf(coptdList.indexOf(arr)+1));
                    copto.setTO004(arr.getTd001());
                    copto.setTO005(arr.getTd002());
                    copto.setTO006(arr.getTd003());
                    copto.setTO007(arr.getTd004());
                    copto.setTO008(arr.getTd005());
                    copto.setTO009(arr.getTd006());
                    copto.setTO010(arr.getTd010());
                    copto.setTO011(arr.getTd007());
                    copto.setTO012(arr.getTd040());
                    copto.setTO013(arr.getTd041());
                    copto.setTO014("");
                    copto.setTO015("");
                    copto.setTO016("");
                    copto.setTO017(0.000000);
                    copto.setTO018("");
                    copto.setTO019(0.000000);
                    copto.setTO020(arr.getTd011());
                    copto.setTO021(arr.getTd012());
                    copto.setTO022(arr.getTd020());
                    copto.setTO023("N");
                    copto.setTO024(arr.getTd008());
                    copto.setTO025(arr.getTd024());
                    copto.setTO026(arr.getTd014());
                    copto.setTO027(arr.getTd026());
                    copto.setTO028(arr.getTd032());
                    copto.setTO029(arr.getTd034());
                    copto.setTO030(arr.getTd032());
                    copto.setTO031(arr.getTd034());
                    copto.setTO032(arr.getTd036());
                    copto.setTO033(arr.getTd023());
                    copto.setTO034(arr.getTd037());
                    copto.setTO035(arr.getTd038());
                    copto.setTO036(arr.getTd039());
                    copto.setTO037("");
                    copto.setTO038("");
                    String s = this.cmsniMapper.queryNI002String(arr.getTd007());
                    if (s!=null){
                        copto.setTO039(s);
                    }else {
                        copto.setTO039("");
                    }
                    copto.setTO039("");
                    copto.setTO046(coptc.getTc077());
                    copto.setTO047("N");
                    this.coptoMapper.insert(copto);
                });
            }
        }catch (Exception e){
            log.error("自动新增出货通知单发生异常"+e.getMessage());
            throw new YiFeiException(500,"自动新增出货通知单发生异常"+e.getMessage());
        }
    }
}
